﻿function  show() {
    $.ajax({
        type:"post",
        url:"/dept/getAllDepart.action",
        data:{pid:0},
        dataType:"json",
        success:function (message) {
            var html="";
            if (message.code==0){
                alert("获取数据失败，请刷新页面重试");
            }else{
                var depts=message.list;
                for (var i = 0; i <depts.length ; i++) {
                    html+="<option value=\""+depts[i].id+"\">"+depts[i].pname+"</option>"
                }
                $("#xueyuan").html(html);
            }
        }
    })
    $.ajax({
        type:"post",
        url:"/dept/getAllDepart.action",
        data:{pid:1},
        dataType:"json",
        success:function (message) {
            var html="";
            if (message.code==0){
                alert("获取数据失败，请刷新页面重试");
            }else{
                var depts=message.list;
                for (var i = 0; i <depts.length ; i++) {
                    html+="<option value=\""+depts[i].id+"\">"+depts[i].pname+"</option>"
                }
                $("#banji").html(html);
            }
        }
    })
    var html1;
    for (var i = 1; i <51; i++) {
        html1+="<option value=\""+i+"\">"+i+"号</option>";
    }
    $("#xuehao").html(html1);
}
function show2() {
    $.ajax({
        type:"post",
        url:"/dept/getAllDepart.action",
        data:{pid:$("#xueyuan").val()},
        dataType:"json",
        success:function (message) {
            var html="";
            if (message.code==0){
                alert("获取数据失败，请刷新页面重试");
            }else{
                var depts=message.list;
                for (var i = 0; i <depts.length ; i++) {
                    html+="<option value=\""+depts[i].id+"\">"+depts[i].pname+"</option>"
                }
                $("#banji").html(html);
            }
        }
    })
}
function show3() {
    var html;
    for (var i = 1; i <51; i++) {
        html+="<option value=\""+i+"\">"+i+"号</option>";
    }
    $("#xuehao").html(html);
}
 function Test1() {
     const stuNo = $("#account").val();
    $.ajax({
        type:"post",
        url:"/common/ExamineStudent.action",
        data:{studentno:stuNo},
        dataType:"json",
        success:function (message) {
            if (message.code==1){
                alert("该用户已注册，请联系管理员")
                $("#registerbutton").attr('disabled',true);
            }else{
                $("#registerbutton").attr('disabled',false);
            }
        }
    })
 }
$(function () {
    jQuery.validator.addMethod("isPhone",function(phone){
        //声明手机号正则表达式
        var reg=/^1[3,4,5,7,8,9]\d{9}$/;
        return reg.test(phone);
    });
    $("#registerForm").validate({
        onsubmit:true,// 是否在提交是验证
        onfocusout:false,// 是否在获取焦点时验证
        onkeyup :false,// 是否在敲击键盘时验证
        rules:{
            "account":{"required":true,"rangelength":[11,11]},
            "username":{"required":true},
            "password":{"required":true,"minlength":8},
            "password2":{"required":true,"equalTo":"#password"},
            "phone":{"required":true,"isPhone":true}
        },
        messages:{
            "account":{"required":"账号不能为空","rangelength":"账号长度为11号"},
            "username":{"required":"用户名不能为空"},
            "password":{"required":"密码不能为空","minlength":"密码最短长度为8"},
            "password2":{"required":"确认密码不能为空","equalTo":"两次输入的密码不一致"},
            "phone":{"required":"手机号不能为空","isPhone":"手机号格式有误!"}
        },
        submitHandler: function(form) {
            Test1();
            $.ajax({
                type:"post",
                url:"/common/register.action",
                data:{name:$("#username").val(),password:$("#password").val(),phone:$("#phone").val(),id:$("#account").val()},
                dataType:"json",
                success:function (message) {
                     if (message.code==0){
                         alert("注册失败，请稍后再试")
                     }else if (message.level==1){
                         alert("注册成功，您的账号为："+$("#accountNo").val()+"将为您跳转到登陆页面")
                         window.location="/index.jsp";
                     }else if (message.level==3){
                         alert("添加账号为："+$("#accountNo").val()+"成功")
                         window.parent.location="/page/admin/adminStudent.jsp";
                     }else{
                         alert("添加账号为："+$("#accountNo").val()+"成功")
                        window.parent.location="/page/teacher/teacherStudent.jsp";
                     }
                }
            })
        }

    })

})

